package com.mdcorbri.helicoptobot.mediators
{
	
	
	import com.mdcorbri.helicoptobot.model.ApplicationModel;
	import com.mdcorbri.helicoptobot.views.MainView;
	
	import flash.data.SQLConnection;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.containers.Canvas;
	import mx.events.DynamicEvent;
	
	import nz.co.codec.flexorm.EntityManager;
	
	
	public class MainViewMediator extends AbstractViewMediator
	{
		
		[Autowire]
		public var applicationModel:ApplicationModel;
		
		protected var entityManager:EntityManager = EntityManager.instance;
		
		private var dynamicEvent:DynamicEvent;
		
		public function MainViewMediator()
		{
			super();
		}
		
		
		
		/**
		 * 
		 */
		public function get view():MainView
		{
			return this._view as MainView;
		}
		
		override protected function setViewListeners():void
		{
			super.setViewListeners();
	
			this.view.cnvs1.addEventListener(MouseEvent.CLICK, handleMouseEvent);
			this.view.cnvs2.addEventListener(MouseEvent.CLICK, handleMouseEvent);
			this.view.cnvs3.addEventListener(MouseEvent.CLICK, handleMouseEvent);
			this.view.cnvs4.addEventListener(MouseEvent.CLICK, handleMouseEvent);
			
			this.view.cnvs5.addEventListener(MouseEvent.CLICK, handleMouseEvent);
			this.view.cnvs6.addEventListener(MouseEvent.CLICK, handleMouseEvent);
			this.view.cnvs7.addEventListener(MouseEvent.CLICK, handleMouseEvent);
			this.view.cnvs8.addEventListener(MouseEvent.CLICK, handleMouseEvent);
		}
		
		override protected function setViewDataBindings():void
		{
			super.setViewDataBindings();
		}
		
		override protected function setViewSecurity():void
		{
			super.setViewSecurity();
			setEntityManager();
		}
		
		private var _database:String;
		public function set database(value:String):void
		{
		
		}
		public function get database():String
		{
			return _database;
		}
		
		/** ************************************************************************
		 *                mediators
		 *  ************************************************************************/
		[Mediate(event="HotSpotClick", properties="hotSpot")]
		public function hotSpotClickHandler(hotSpot:String):void
		{
			switch(hotSpot)
			{
				case "robotGauge":
				this.view.mainViewStack.selectedIndex = 1;
				break;
				
				case "rightRobotEyeHotSpot":
				this.view.mainViewStack.selectedIndex = 1;
				break;
				
				case "leftRobotEyeHotSpot":
				this.view.mainViewStack.selectedIndex = 1;
				break;
			}
		}
		 
		 
		 
		
		/** ************************************************************************
		 *                handlers
		 *  ************************************************************************/
		
	
		 
		 /** ************************************************************************
		 *                utils
		 *  ************************************************************************/
		 //set up the ORM entity manager
		private function setEntityManager():void
		{
			var dbFile:File = File.applicationStorageDirectory.resolvePath( "helicoptobot.db" );
			var sqlConnection:SQLConnection = new SQLConnection();
			sqlConnection.open( dbFile );
			entityManager.sqlConnection = sqlConnection;
			
			// place ORM entity manager into the model
			applicationModel.entityManager = entityManager;
		}
		
		private function handleMouseEvent(mouseEvent:MouseEvent):void
		{
			var id:String = Canvas(mouseEvent.currentTarget).id;
			
			switch(id)
			{
				case "cnvs1":
				this.view.mainViewStack.selectedIndex = 0;
				break;
				
				case "cnvs2":
				this.view.mainViewStack.selectedIndex = 1;
				break;
				
				case "cnvs3":
				this.view.mainViewStack.selectedIndex = 2;
				break;
				
				case "cnvs4":
				this.view.mainViewStack.selectedIndex = 3;
				break;
				
				case "cnvs5":
				this.view.mainViewStack.selectedIndex = 0;
				break;
				
				case "cnvs6":
				this.view.mainViewStack.selectedIndex = 1;
				break;
				
				case "cnvs7":
				this.view.mainViewStack.selectedIndex = 2;
				break;
				
				case "cnvs8":
				this.view.mainViewStack.selectedIndex = 3;
				break;
			}
		}
		
		
		
		/**
		 * Remove any listeners we've created.
		 */
		override protected function cleanup( event:Event ):void
		{
			super.cleanup(event);
		}

	}
}